'use strict';
app.service('registerService',['$http','$q','utilService',function($http,$q,utilService){

    this.shopRegisterBaseinfo = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'shop/register/baseinfo',
            data:$scope.baseinfo,
            method:"POST"
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    this.shopRegisterEnregisterPersonal = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'shop/register/enregister/personal',
            data:$scope.enregisterform,
            method:"POST"
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    this.shopRegisterEnregisterEnterprise = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'shop/register/enregister/enterprise',
            data:$scope.enregisterform,
            method:"POST"
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }


    //上传文件
    this.uploadFile=function($scope,data){
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: $scope.app.domain+'upload',
            data:data,
            headers: {'Content-Type': undefined},
            transformRequest: angular.identity
        }).success(function(response) {
            deferred.resolve(response.data);
        }).error(function(e){
            console.log(e);
        });
        return deferred.promise;
    }

    //删除文件
    this.deleteFile=function($scope){
        utilService.api({
            url: $scope.app.domain+'upload/delete',
            data:{'filefullPath':$scope.filefullPath},
            method:"POST"
        }).then(function(response) {
            console.info(response)
        });

    }


    //查询行业
    this.getIndustryList = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'industry'
        }).then(function(response){
                deferred.resolve(response.data);
            },function(e){
                console.error(e);
                deferred.reject("连接服务器出错！");
            });
        return deferred.promise;
    }

    //注册-选择套餐
    this.addSetShopMeal = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'shop/register/meal',
            data:{setMealId:$scope.setMealId},
            method:"POST"
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    this.addSetChainMeal = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'chain',
            data:{setMealId:$scope.setMealId},
            method:"PUT"
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }



    this.getSetMealList = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'set/meal/list/',
            params:$scope.setMealVO
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    }


    this.getMobilePhoneCode = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'getMobilePhoneCode',
            params:{mobilePhone:$scope.baseinfo.phone}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    }

    this.checkPhone = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'system/user/check/mobilePhone',
            params:{mobilePhone:$scope.baseinfo.phone}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    }

    //查询行业
    this.getIndustryFullName = function($scope){
        var deferred = $q.defer();
        $http.get($scope.app.domain + 'shop/get/industry/list'
        ).then(function(reslut){
                deferred.resolve(reslut.data);
            },function(e){
                deferred.reject("连接服务器出错");
            });
        return deferred.promise;
    }


    //创建店铺
    this.registerShopAdd = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'shop/register/add',
            data:$scope.shop,
            method:"POST"
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    //发送请求回后台获取地区数据
    this.findArea=function($scope,level,parentId){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain+"area/info",
            params:{level:level,parentId:parentId}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        })

        return deferred.promise;
    }


    //创建连锁
    this.addChain=function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'chain',
            data:$scope.chain
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    //查询店铺信息
    this.findByShopId= function ($scope) {
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'shop/info'
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        })

        return deferred.promise;
    }


    this.updateShopBusiness=function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain+"/shop/register/business",
            data:$scope.shopInfo,
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }


    this.delImage=function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain+"/shop/info",
            data:{imgType:$scope.imgType,id:$scope.shopInfo.id},
            method:'DELETE'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    this.findSystemUser=function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'system/user/'+$scope.id,
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        })
        return deferred.promise;
    }

}]);
